package com.gopay.bis.icbc.ebank.impl;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.bis.icbc.ebank.operation.IcbcB2cQuery;
import com.gopay.bis.icbc.ebank.operation.IcbcWapCertHelper;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.icbc.ebank.pay.IcbcB2cMessage;
import com.gopay.common.domain.bank.icbc.ebank.pay.res.PayWapRes;
import com.gopay.common.domain.bank.icbc.ebank.query.res.CmsQueryB2CRes;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.businesses.BankService;


/** 
 * @ClassName: IcbcWapBankServiceImpl 
 * @Description: 工行wap处理类
 * @author w_jiqing 
 * @date 2016年8月25日 下午3:25:32  
 */ 
public class IcbcWapBankServiceImpl implements BankService {
	private static Log logger = LogFactory.getLog(IcbcWapBankServiceImpl.class);
	Logger log = LoggerFactory.getLogger(IcbcWapBankServiceImpl.class);
	private IcbcB2cQuery icbcB2cQuery;
	private IcbcWapCertHelper icbcWapCertHelper;

	public IcbcB2cMessage sign(BaseMessage message) throws GopayException {
		IcbcB2cMessage messageb2c = (IcbcB2cMessage) message;
		Object objectRes = null;
		objectRes = icbcWapCertHelper.sign(messageb2c);
		String signMessage = (String) objectRes;
		logger.error("icbc sign signmessage:" + signMessage);
		String tranData = icbcWapCertHelper.getTranData(messageb2c.getPlainMessage());
		messageb2c.setTranData(tranData);
		messageb2c.setMercert(icbcWapCertHelper.getPk(messageb2c.getPublicKeyPath()));
		messageb2c.setSignMessage(signMessage);
		return messageb2c;
	}

    public Object verify(BaseMessage message) throws GopayException {
		Map<String, Object> map = new HashMap<String, Object>();
		IcbcB2cMessage b2cMessage = (IcbcB2cMessage) message;
		Object objectRes = null;
		objectRes = icbcWapCertHelper.verify(b2cMessage);
		PayWapRes res =icbcWapCertHelper.parse(b2cMessage);
	    map.put("res", res);
	    map.put("flag", objectRes);
	    logger.error("icbc service verify flag:" + objectRes);
	    logger.info("icbc service verify res:"+ ToStringBuilder.reflectionToString(res));				
		return map;
	}

	@SuppressWarnings("unchecked")
	public Object query(Object object) throws GopayException {
		Map<String, String> param = (Map<String, String>) object;
		long start =System.currentTimeMillis();
		
		String responseBody = icbcB2cQuery.sendReq(param);
		logger.error("process time >>>>>>>> "+(System.currentTimeMillis() - start));
		CmsQueryB2CRes res = new CmsQueryB2CRes();
		// 截取返回消息并做base64转码
		String plainMessage = icbcB2cQuery.decode(responseBody);
		// 将XML字符串转换为对象
		res = (CmsQueryB2CRes)icbcB2cQuery.parse(plainMessage);
		return res;
	}


	public void setIcbcB2cQuery(IcbcB2cQuery icbcB2cQuery) {
		this.icbcB2cQuery = icbcB2cQuery;
	}

	public IcbcWapCertHelper getIcbcWapCertHelper() {
        return icbcWapCertHelper;
    }

    public void setIcbcWapCertHelper(IcbcWapCertHelper icbcWapCertHelper) {
        this.icbcWapCertHelper = icbcWapCertHelper;
    }

    @Override
	public Object dayend(Object obj) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object refund(Object obj) throws GopayException {
		// TODO Auto-generated method stub
		return null;
	}

}
